<script>
import { addHealthRecordsDetail } from 'api/health_records'

export default {
  name: 'Form',
  data() {
    return {
      formInline: {
        weight: '', // 体重，单位kg
        height: '', // 身高，单位cm
        blood_pressure: '', // 血压，单位MmHg
        blood_sugar: '', // 血糖，单位mg/dL
        pulse: '', // 脉搏，单位次/分
        heart_rate: '', // 心率，单位Bpm
        calorie: '' // 卡路里，单位千卡
      },
      rules: {
        weight: [{ required: true, message: '请输入体重', trigger: 'blur' }],
        height: [{ required: true, message: '请输入身高', trigger: 'blur' }],
        blood_pressure: [
          { required: true, message: '请输入血压', trigger: 'blur' }
        ],
        blood_sugar: [
          { required: true, message: '请输入血糖', trigger: 'blur' }
        ],
        pulse: [{ required: true, message: '请输入脉搏', trigger: 'blur' }],
        heart_rate: [
          { required: true, message: '请输入心率', trigger: 'blur' }
        ],
        calorie: [{ required: true, message: '请输入卡路里', trigger: 'blur' }]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.formInline.validate(async valid => {
        if (valid) {
          const res = await addHealthRecordsDetail(this.formInline)
          if (res?.code === 200) {
            this.$message({
              message: '提交成功',
              type: 'success'
            })
            this.$router.back()
          }
        } else {
          this.$message({
            message: '请填写完整信息',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<template>
  <div class="container">
    <el-form
      ref="formInline"
      :inline="false"
      :rules="rules"
      :model="formInline"
      class="demo-form-inline"
    >
      <el-form-item label="体重（kg）" prop="weight">
        <el-input
          v-model="formInline.weight"
          type="number"
          placeholder="请输入体重"
        />
      </el-form-item>
      <el-form-item label="身高（cm）" prop="height">
        <el-input
          v-model="formInline.height"
          type="number"
          placeholder="请输入身高"
        />
      </el-form-item>
      <el-form-item label="血压（MmHg）" prop="blood_pressure">
        <el-input
          v-model="formInline.blood_pressure"
          type="number"
          placeholder="请输入血压"
        />
      </el-form-item>
      <el-form-item label="血糖（mg/dl）" prop="blood_sugar">
        <el-input
          v-model="formInline.blood_sugar"
          type="number"
          placeholder="请输入血糖"
        />
      </el-form-item>
      <el-form-item label="脉搏（次/分）" prop="pulse">
        <el-input
          v-model="formInline.pulse"
          type="number"
          placeholder="请输入脉搏"
        />
      </el-form-item>
      <el-form-item label="心率（Bpm）" prop="heart_rate">
        <el-input
          v-model="formInline.heart_rate"
          type="number"
          placeholder="请输入心率"
        />
      </el-form-item>
      <el-form-item label="卡路里（千卡）" prop="calorie">
        <el-input
          v-model="formInline.calorie"
          type="number"
          placeholder="请输入卡路里"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="$router.back()">返回</el-button>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped lang="scss">
.container {
  width: 694px;
  margin: 0 auto;
  background-color: #fff;
  padding: 20px;
}
</style>
